﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Entity;
using HanGu.Common;

namespace HanGu.CRM.Areas.Module.Controllers
{
    public class DefaultController : AdminBaseController
    {
        //
        // GET: /Module/
        List<HanGu.Entity.Module> GetModuleList()
        {
            List<HanGu.Entity.Module> moduleList = HelperCommon.GetModulesFromXML();// serviceModule.GetAll(base.SearchParma);
            HttpContext.Cache["moduleList"] = moduleList;
            return moduleList;
        }
        public ActionResult Index()
        {
            return View(GetModuleList());
        }

        [HttpPost]
        public string Index(FormCollection collection)
        {
            try
            {
                var moduleList = HttpContext.Cache["moduleList"] as List<HanGu.Entity.Module>;
                var retult = false;
                if (moduleList != null && moduleList.Count > 0)
                {
                    retult = serviceModule.SaveAll(moduleList);

                }
                if (retult)
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        #region old
        //SelectList GetDropModuleList()
        //{
        //    var list = serviceModule.GetAll();
        //    list.Add(new HanGu.Entity.Module() { Title = "--------请选择--------" });
        //    SelectList selectList = new SelectList(list, "ID", "Title");
        //    return selectList;

        //}
        //public ActionResult Create(string id)
        //{
        //    HanGu.Entity.Module module = new HanGu.Entity.Module() { GUID = Helper.GetGuid };
        //    //if (id > 0)
        //    {
        //        module = serviceModule.GetByID(id);
        //    }
        //    //SearchParma.IntObj1 = id;
        //    ViewData["ModuleList"] = GetDropModuleList();
        //    return View(viewCreate, module);
        //}

        ////
        //// POST: /Module/Create

        //[HttpPost]
        //[ValidateInput(false)]
        //public string Create(FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add insert logic here
        //        HanGu.Entity.Module module = new HanGu.Entity.Module();
        //        TryUpdateModel(module, collection);
        //        if (!string.IsNullOrEmpty(module.ID))
        //        {
        //            serviceModule.Update(module);
        //        }
        //        else
        //        {
        //            serviceModule.Add(module);
        //        }
        //        ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");
        //    }
        //    catch (Exception ex)
        //    {
        //        base.ExceptionHand(ex);
        //        ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
        //    }
        //    return "{" + ajaxStr + "}";
        //}

        ////
        //// GET: /Module/Edit/5

        //public ActionResult Edit(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /Module/Edit/5

        //[HttpPost]
        //public ActionResult Edit(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add update logic here

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        ////
        //// GET: /Module/Delete/5
        //[HttpPost]
        //public string Delete(string id, string guid)
        //{
        //    //if (id < 10)
        //    //{
        //    //    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SystemDelFail, navTabId, "", "");
        //    //}
        //    //else
        //    {
        //        try
        //        {
        //            serviceModule.Delete(id);
        //            ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
        //        }
        //        catch (Exception ex)
        //        {
        //            ExceptionHand(ex);
        //            ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
        //        }
        //    }

        //    return "{" + ajaxStr + "}";

        //}
        #endregion
    }
}
